<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Villager;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;

// 村民
class VillagerController extends Controller
{
    // 列表
    public function index(Request $request)
    {
        $is_all = $request->input('is_all', 0); //不分页获取全部数据

        $keywords = $request->input('keywords', ''); // 查询关键字
        $grid_id = $request->input('grid_id', '');

        if($is_all){
            $data = Villager::query()
            ->when($keywords, function ($query) use ($keywords) {
                $query->where('name', 'like', "%{$keywords}%");
            })
            ->when($grid_id, function ($query) use ($grid_id) {
                $query->whereIn('grid_id', $grid_id);
            })
            ->where($this->getAppWhere())
             ->orderBy('id')
            ->get()
            ->toArray();
        }else{
            $data = Villager::query()
                ->when($keywords, function ($query) use ($keywords) {
                    $query->where('name', 'like', "%{$keywords}%");
                })
                ->when($grid_id, function ($query) use ($grid_id) {
                    $query->whereIn('grid_id', $grid_id);
                })
                ->where($this->getAppWhere())
                ->orderByDesc('id')
                ->paginate(env('PAGE_SIZE', 10));
        }    
        return showData($data);    
    }
}

